package com.fcl.mylibrary.webview;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewConfiguration;
import android.webkit.WebSettings;
import android.webkit.WebView;

/**
 * Created by Administrator on 2016/10/26.
 */
public class MyWebView extends WebView {

    public MyWebView(Context context) {
        super(context);
        init();
    }

    public MyWebView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        setClickable(false);
        WebSettings settings = getSettings();

        settings.setJavaScriptEnabled(true); //支持js

        settings.setBuiltInZoomControls(false); //是否显示缩放工具
        settings.setSupportZoom(false); //是否支持缩放

        //图片自适应屏幕
//        settings.setUseWideViewPort(true); //设置推荐使用的窗口
//        settings.setLoadWithOverviewMode(true); //设置加载页面的模式

        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);

//        settings.setDefaultFontSize(16);
//        settings.setMinimumFontSize(16);

    }

    public void loadData(String content) {
        String data = dealData(content);
        //因为直接添加外部css没有生效才这样写，这样重复地方太多
        data = data.replaceAll("<img", "<img width=100%");
        data = data.replaceAll("<a", "<a style=\"color:#00dbc4;\"");
        loadData(data, "text/html;charset=utf-8", null);
    }

    private String dealData(String data) {
        //直接这样加进去没有生效
        return "<head> <link rel=\"stylesheet\" type=\"text/css\" href=\"file:///android_asset/detail.css\"> </head>" + data;
    }

    long preTouchTime=0;
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction()==MotionEvent.ACTION_UP) {
            long currentTouchTime=System.currentTimeMillis();
            if (currentTouchTime-preTouchTime<= ViewConfiguration.getDoubleTapTimeout()) {
                preTouchTime = currentTouchTime;
                return true;
            }
            preTouchTime=currentTouchTime;
        }
        return super.dispatchTouchEvent(ev);
    }

}
